/**
 * @Copyright 2012, Founder Mobile Media Technology（Chongqing） Co., Ltd L.P. All
 *            rights reserved. The information contained herein is confidential
 *            and proprietary to Founder Mobile Media Technology（Chongqing） Co.,
 *            Ltd, and considered a trade secret as defined under civil and
 *            criminal statutes. Founder Mobile Media Technology（Chongqing） Co.,
 *            Ltd shall pursue its civil and criminal remedies in the event of
 *            unauthorized use or misappropriation of its trade secrets. Use of
 *            this information by anyone other than authorized employees of
 *            Founder Mobile Media Technology（Chongqing） Co., Ltd is granted
 *            only under a written non-disclosure agreement, expressly
 *            prescribing the scope and manner of such use.
 */
package com.itttq.kb.cmpt;

import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.springframework.stereotype.Component;

/**
 * 发送邮件
 * 
 * @createTime: Jun 19, 2013 10:08:01 AM
 * @author: <a href="mailto:gaoxg@FOUNDER.COM.CN">Xuguo Gao</a>
 * @version: 0.1
 * @lastVersion: 0.1
 * @updateTime:
 * @updateAuthor: <a href="mailto:gaoxg@FOUNDER.COM.CN">Xuguo Gao</a>
 * @changesSum:
 * 
 */
@Component
public class EmailTools {
	// 先代码写死
	private  String host = "smtp.qq.com"; // smtp服务器
	private String port = "465"; // smtp服务器
	private String from = "xxxx@qq.com"; // 发送方
	private String username = "xxxx@qq.com"; // 服务器用户名
	private String password = "您的QQ邮箱密码"; // 服务器密码

	/**  
     * 发送邮件. 
     * @return boolean - 发送结果  
     */  
    public boolean sendMail(String to, String subject, String body) {
        if (to == null || subject == null   || body == null) { return false; }
        try {
            Properties props = new Properties();
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            //如果没有这行代码会出现这样的错误:Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1
            props.put("username", username);
            props.put("password", password); //QQ邮箱的SMTP授权码 (不是QQ的密码)
            props.put("mail.transport.protocol", "smtp" );
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", true);
            props.put("mail.smtp.port", port);//qq的SMTP端口是465  
  
            Session mailSession = Session.getDefaultInstance(props);  
            Message msg = new MimeMessage(mailSession);   
  
            msg.setFrom(new InternetAddress(from));  
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));  
            msg.setSentDate(new Date());  
            msg.setSubject(subject);   
  
//            msg.setText(body);
            
            BodyPart bodyPart = new MimeBodyPart();//BodyPart是邮件内容的承载体，可以是文件，图片，附件等...  
            bodyPart.setContent(body, "text/html;charset=UTF-8");//设置页面的编码值  
            Multipart multiPart = new MimeMultipart();//Multipart又是BodyPart的承载体，一个multiPart可以包含多个BodyPart  
            multiPart.addBodyPart(bodyPart);//将bodyPart添加到multiPart  
            msg.setContent(multiPart);// 将MultiPart设为邮件内容主体msg的  
            
            msg.saveChanges();  
            Transport transport = mailSession.getTransport("smtp");  
            transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("username"), props.getProperty("password"));   
            transport.sendMessage(msg, msg.getAllRecipients());  
            transport.close();   
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
        return true;  
    } 
    
    public static void main(String[] args) {
    	new EmailTools().sendMail("252909344@qq.com", "test", "ss");
    }  
}
